﻿
using System;
using System.Linq.Expressions;

namespace YiDa.Domain.Specifications
{
    /// <summary>
    /// Represents the specification which is represented by the corresponding
    /// LINQ expression.
    /// </summary>
    /// <typeparam name="T">The type of the object to which the specification is applied.</typeparam>
    /// <remarks>
    /// 只是为了将Lambda表达式作为Specification进行使用,构建单一值的时候需要先从此实例开始
    /// 例如（a==1） && (b==2) 应该构建成new AndSpecification(new ExpressionSpecification(a==1),new ExpressionSpecification(b==2))    
    /// </remarks>
    internal sealed class ExpressionSpecification<T> : Specification<T>
    {        
        private Expression<Func<T, bool>> expression;        
        
        /// <summary>
        /// Initializes a new instance of <c>ExpressionSpecification&lt;T&gt;</c> class.
        /// </summary>
        /// <param name="expression">The LINQ expression which represents the current
        /// specification.</param>
        public ExpressionSpecification(Expression<Func<T, bool>> expression)
        {
            this.expression = expression;
        }        
        
        /// <summary>
        /// Gets the LINQ expression which represents the current specification.
        /// </summary>
        /// <returns>The LINQ expression.</returns>
        public override Expression<Func<T, bool>> GetExpression()
        {
            return this.expression;
        }
        
    }
}
